﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TodayWhoModel;

namespace TodayWho
{
    public partial class r : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HandleRedirect();
        }

        private void HandleRedirect()
        {
            if (string.IsNullOrEmpty(Request.QueryString["r"]))
            {
                Response.Redirect("home.aspx");
            }
            else if (string.IsNullOrEmpty(Request.QueryString["u"]) || string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                Response.Redirect(Request.QueryString["r"]);
            }
            else
            {
                UserModel userModel = new UserModel();
                userModel.UserName = Request.QueryString["u"];
                if (!string.IsNullOrEmpty(userModel.UserName))
                {
                    userModel.RetrieveUserIdWithUserName();
                    if (!string.IsNullOrEmpty(userModel.UserName))
                    {
                        UserServiceStatsModel userServices = new UserServiceStatsModel();
                        userServices.UserId = userModel.UserId;
                        userServices.ServiceName = Request.QueryString["s"];
                        userServices.UpdateServiceCount();
                        Response.Redirect(Request.QueryString["r"]);
                    }
                }
            }        
        }
    }
}